<template>
  <view class="login bg-blue flex-sub padding flex-center flex-direction">
    <view class="margin text-bold" style="margin-top: -120px; font-size: 40px"
      >爆 单 王</view
    >
    <u-form
      class="padding w100 bg-white bd-r"
      :model="form"
      :rules="rules"
      ref="uForm"
    >
      <u-form-item label="账号" prop="account">
        <u-input suffixIcon="account" v-model="form.account" />
      </u-form-item>
      <u-form-item label="密码" prop="password">
        <u-input suffixIcon="lock" v-model="form.password" type="password" />
      </u-form-item>
      <!-- <u-form-item prop="role">
        <u-radio-group v-model="form.role" class="flex justify-between">
          <u-radio
            v-for="(item, index) in radiolist1"
            :key="index"
            :label="item.label"
            :name="item.value"
          >
          </u-radio>
        </u-radio-group>
      </u-form-item> -->
    </u-form>
    <u-button class="margin-top-xl" @click="submit">登录</u-button>
  </view>
</template>

<script>
import { userLogin } from "@/api";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      form: {
        account: "",
        password: "",
        role: 3,
      },
      radiolist1: [
        {
          label: "管理员",
          value: 1,
        },
        {
          label: "代理商",
          value: 2,
        },
        {
          label: "公司",
          value: 3,
        },
      ],
      rules: {
        account: [
          {
            required: true,
            message: "请输入账号",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["isAdmin", "curProStatus"]),
  },
  onLoad() {
    const userInfo = uni.getStorageSync("__userInfo__");
    console.log("userInfo", userInfo);
    if (userInfo) {
      return uni.switchTab({
        url: "/pages/index/index",
      });
    }
    if (!this.isAdmin) {
      uni.setStorageSync("curProStatus", "pdd");
      this.$store.commit("SET_CUR_PRO_STATUS", "pdd");
    }
  },
  methods: {
    submit() {
      this.$refs.uForm
        .validate()
        .then((res) => {
          if (this.form.account === "admin") {
            this.form.role = 1;
          }
          userLogin({
            channel: this.isAdmin ? 0 : this.curProStatus == "pdd" ? 1 :this.curProStatus == "jd" ?4: 3,
            ...this.form,
          }).then(({ data }) => {
            uni.setStorageSync("__token__", data.token);
            uni.setStorageSync("__userInfo__", data);

            return uni.switchTab({
              url: "/pages/index/index",
            });
          });
        })
        .catch((errors) => {
          console.error(errors);
          uni.$u.toast("表单校验失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  background-image: url("~@/static/bg.png");
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
